package jscinttools.scint;

import jscinttools.utilities.jhplotutilities.FigureParametersJHPlot;
import jscinttools.utilities.aidautilities.AidaFileReader;
//import jscinttools.utilities.aidautilities.GrabIObject;
//import jscinttools.scint.datafit.*;

import jscinttools.datafit.FitSettings;
import jscinttools.datafit.DataFitter;
import jscinttools.datafit.FitFunction;
import jscinttools.datafit.scint.GaussMoyalFitFunc;

import hep.aida.IHistogram1D;
import hep.aida.IFunction;
import hep.aida.IFitResult;
import hep.aida.IFitter;
import hep.aida.IFitParameterSettings;

import java.lang.String;
import java.io.FileNotFoundException;
import java.io.IOException;

import jhplot.DrawOptions;
import jhplot.H1D;
import jhplot.F1D;

/**
 *  Description of the Class
 *
 *@author     Marc-Antoine Verdier
 *@created    3 avril 2011
 */
public class Spectrum implements FigureParametersJHPlot
{
	private H1D spectrum = null;
	private F1D fitFunc = null;
	private F1D fitFuncZoom = null;
	private IFitResult fitRes = null;

	private String specPath = null;
	private String fullFileName = null;
	//private double[] startParam = null;
	private FitSettings fitSet;
	private boolean sumTwoChan = false;
	private double[] winZoom = null;


	/**
	 *  Constructor for the Spectrum object
	 *
	 *@param  fullFileName               Description of the Parameter
	 *@param  winZoom                    Description of the Parameter
	 *@param  sumTwoChan                 Description of the Parameter
	 *@param  fitSet                     Description of the Parameter
	 *@exception  FileNotFoundException  Description of the Exception
	 *@exception  IOException            Description of the Exception
	 */
	public Spectrum(String fullFileName, FitSettings fitSet, double[] winZoom, boolean sumTwoChan)
			 throws FileNotFoundException, IOException
	{
		this.fullFileName = fullFileName;
		this.fitSet = fitSet;
		this.winZoom = winZoom;
		this.sumTwoChan = sumTwoChan;

		if (!sumTwoChan)
		{
			this.specPath = "Reanalysis/Number of Single Photons, Ch0, All Events";
		}
		else
		{
			this.specPath = "Reanalysis/Ch1 Plus Ch0 Total Photons,All Events";
		}

		//IHistogram1D specHist = new GrabIObject(new AidaFileReader(fullFileName).getTree(), specPath).getHisto1D();
		IHistogram1D specHist = new AidaFileReader(fullFileName).grabHisto1D(specPath);
		specHist.scale(1.0 / specHist.maxBinHeight());

		this.spectrum = new H1D(specHist);
		spectrum.setTitle("Spectrum");
		spectrum.setStyle("h");
		spectrum.setColor(colors[0]);
		spectrum.setPenWidth(2);
		spectrum.setErrAll(false);

		if (fitSet != null)
		{
			//ScintDataFit sdf = new ScintDataFit(new SpectrumFit(startParam));
			//sdf.doFit(specHist);
			//this.fitRes = sdf.getFitResult();

			DataFitter df = new DataFitter(new GaussMoyalFitFunc());
			this.fitRes = df.doFit(specHist, fitSet);

			this.fitFunc = new F1D("Fit", fitRes.fittedFunction(), spectrum.getMin(), spectrum.getMax());
			fitFunc.setPoints(1000);
			fitFunc.setColor(colors[1]);
			fitFunc.setPenWidth(2);

			if (winZoom != null)
			{
				this.fitFuncZoom = new F1D("FitZoom", fitRes.fittedFunction(), winZoom[0], winZoom[1]);
				fitFuncZoom.setPoints(1000);
				fitFuncZoom.setColor(colors[1]);
				fitFuncZoom.setPenWidth(2);
			}
		}
	}


	/**
	 *  Constructor for the Spectrum object
	 *
	 *@param  fullFileName               Description of the Parameter
	 *@param  winZoom                    Description of the Parameter
	 *@param  fitSet                     Description of the Parameter
	 *@exception  FileNotFoundException  Description of the Exception
	 *@exception  IOException            Description of the Exception
	 */
	public Spectrum(String fullFileName, FitSettings fitSet, double[] winZoom)
			 throws FileNotFoundException, IOException
	{
		this(fullFileName, fitSet, winZoom, false);
	}


	/**
	 *  Constructor for the Spectrum object
	 *
	 *@param  fullFileName               Description of the Parameter
	 *@param  fitSet                     Description of the Parameter
	 *@exception  FileNotFoundException  Description of the Exception
	 *@exception  IOException            Description of the Exception
	 */
	public Spectrum(String fullFileName, FitSettings fitSet)
			 throws FileNotFoundException, IOException
	{
		this(fullFileName, fitSet, null, false);
	}


	/**
	 *  Constructor for the Spectrum object
	 *
	 *@param  fullFileName               Description of the Parameter
	 *@param  sumTwoChan                 Description of the Parameter
	 *@param  fitSet                     Description of the Parameter
	 *@exception  FileNotFoundException  Description of the Exception
	 *@exception  IOException            Description of the Exception
	 */
	public Spectrum(String fullFileName, FitSettings fitSet, boolean sumTwoChan)
			 throws FileNotFoundException, IOException
	{
		this(fullFileName, fitSet, null, sumTwoChan);
	}


	/**
	 *  Constructor for the Spectrum object
	 *
	 *@param  fullFileName               Description of the Parameter
	 *@param  sumTwoChan                 Description of the Parameter
	 *@exception  FileNotFoundException  Description of the Exception
	 *@exception  IOException            Description of the Exception
	 */
	public Spectrum(String fullFileName, boolean sumTwoChan)
			 throws FileNotFoundException, IOException
	{
		this(fullFileName, null, null, sumTwoChan);
	}


	/**
	 *  Constructor for the Spectrum object
	 *
	 *@param  fullFileName               Description of the Parameter
	 *@exception  FileNotFoundException  Description of the Exception
	 *@exception  IOException            Description of the Exception
	 */
	public Spectrum(String fullFileName)
			 throws FileNotFoundException, IOException
	{
		this(fullFileName, null, null, false);
	}


	/**
	 *  Gets the spectrum attribute of the Spectrum object
	 *
	 *@return    The spectrum value
	 */
	public H1D getSpectrum()
	{
		return spectrum;
	}


	/**
	 *  Gets the fitFunc attribute of the Spectrum object
	 *
	 *@return    The fitFunc value
	 */
	public F1D getFitFunc()
	{
		return fitFunc;
	}


	/**
	 *  Gets the fitZoom attribute of the Spectrum object
	 *
	 *@return    The fitZoom value
	 */
	public F1D getFitFuncZoom()
	{
		return fitFuncZoom;
	}


	/**
	 *  Gets the fitRes attribute of the Spectrum object
	 *
	 *@return    The fitRes value
	 */
	public IFitResult getFitRes()
	{
		return fitRes;
	}
}

